# encoding:utf-8
import datetime
import time


def get_before_time(timestamp, hours, format='%Y-%m-%d %H:%M:%S'):
    """
    以给定时间戳为基准，后退 hours 个小时得到对应的时间戳
    :param timestamp:
    :param hours:
    :param format:
    :return:
    """
    now_time = datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
    for i in range(hours):
        now_time -= datetime.timedelta(hours=1)
    next_timestamp = now_time.strftime('%Y-%m-%d %H:%M:%S')
    return next_timestamp


def get_future_time(timestamp, hours, format='%Y-%m-%d %H:%M:%S'):
    """
    以给定时间戳为基准，前进 hours 个小时得到对应的时间戳
    """
    now_time = datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
    for i in range(hours):
        now_time += datetime.timedelta(hours=1)
    next_timestamp = now_time.strftime('%Y-%m-%d %H:%M:%S')
    return next_timestamp


def get_after_timestamp_ms(current_timestamp_ms, seconds):
    # 将当前时间戳转换为秒级时间戳
    current_timestamp_s = current_timestamp_ms // 1000
    # 计算6小时后的时间戳（以秒为单位）
    six_hours_later_timestamp_s = current_timestamp_s + seconds
    # 将6小时后的时间戳转换为毫秒级时间戳
    six_hours_later_timestamp_ms = six_hours_later_timestamp_s * 1000
    return six_hours_later_timestamp_ms


def get_time_stamp10(datetime_obj):
    """生成10位时间戳   eg:1557842287"""
    # 10位，时间点相当于从1.1开始的当年时间编号
    return str(int(time.mktime(datetime_obj.timetuple())))


def get_time_stamp13(datetime_obj):
    """生成13位时间戳   eg:1557842287123"""
    time_stamp_10 = get_time_stamp10(datetime_obj)
    # 3位，微秒
    time_microsecond = str("%06d" % datetime_obj.microsecond)[0:3]
    time_stamp = time_stamp_10 + time_microsecond
    return int(time_stamp)


def get_time_stamp10_now():
    """生成当前时间的10位时间戳   eg:1557842287"""
    return get_time_stamp10(datetime.datetime.now())


def get_time_stamp13_now():
    """生成当前时间的13位时间戳   eg:1557842287123"""
    return get_time_stamp13(datetime.datetime.now())


if __name__ == '__main__':
    # timestamp = '2018-12-19 11:00:00'
    # print(get_before_time("1695282458", 2, format='%Y-%m-%d %H:%M:%S'))
    # print(get_future_time(timestamp, 2, format='%Y-%m-%d %H:%M:%S'))
    print(get_after_timestamp_ms(int(time.time() * 1000), 6*60*60))
